var LOG = LOG || {};

/**
 * 日志管理-用户登陆日志
 */
LOG.userLogin = Ext.extend(Ext.Panel,{
	closable:true
	,width:'100%'
	,initComponent:function(){
		this.tbar = this.buildBbar(this);
		this.items = this.buildGrid();
		LOG.userLogin.superclass.initComponent.call(this);
	}
	,buildBbar: function(mp) {
   		return [{
   				text: "删除日志",
   				handler:this.deleteLog,
    			iconCls: 'ico-task-delete'
	   		},'->',{
	   			xtype:'label',text:'开始时间：'
	   		},{
	   			xtype:'datefield',
	   			format:'Y-m-d',
	   			emptyText:'请选择开始时间',
	   			width:120,
	   			ref:'../startTime'
	   			,listeners:{
	                 'specialkey':function(f,e) {
	                 	 if(e.getKey() == e.ENTER){
	                 	 		var doSearch = mp.search ;
	                 	 		mp.doSearch(doSearch);
	                 	 }
	                 }
   				}
	   		},{xtype:'spacer',width:10},{
	   			xtype:'label',text:'结束时间：'
	   		},{
	   			xtype:'datefield',
	   			format:'Y-m-d',
	   			emptyText:'请选择结束时间',
	   			width:120,
	   			ref:'../endTime'
	   			,listeners:{
	                 'specialkey':function(f,e) {
	                 	 if(e.getKey() == e.ENTER){
	                 	 		var doSearch = mp.search ;
	                 	 		mp.doSearch(doSearch);
	                 	 }
	                 }
   				}
	   		},{xtype:'spacer',width:10},{
   				 xtype:'logTextField',
   				 ref: '../searchText',
   				 width:200,
   				 value:'',
	             emptyText:'双击获得可查询员工列表',
	             readOnly:true,
	             listeners:{
	                 'specialkey':function(f,e) {
	                 	 if(e.getKey() == e.ENTER){
	                 	 		var doSearch = mp.search ;
	                 	 		mp.doSearch(doSearch);
	                 	 }
	                 }
   				}
		   		,onDblClick:function(){
					new LOG.userLogin.validStaff({textField:this}).show();
				}
   			}
   			,{xtype:'spacer',width:10},{
   				text : "查询",
   				ref:"../search",
   				handler :this.doSearch,
   				iconCls: 'ico-dot-search'
   			 }] ;
   	}
	,buildGrid: function(){
//		var thisGrid = this;
   	 	var selectMode=new Ext.grid.CheckboxSelectionModel({
					dataIndex: "ID",
	                sortable: false,
	                singleSelect:false,
	                width:20
				});
   	    var userLoginStore =new Ext.data.JsonStore({
	         totalProperty:'totalProperty'
        	 ,url:'log_getSystemLogs.action'
        	 ,fields:['ID','LOGIN_TIME','USER_NAME','IP','LOG_CONTENT','LOGOUT_TIME']
        	 ,root:'root'
        });
		return[{
			xtype:'grid'
			,viewConfig:{
				forceFit:true
			}
			,store:userLoginStore
			,sm:selectMode
			,bbar:[new Ext.PagingToolbar({
					pageSize : 25,
					store : userLoginStore,
					displayInfo : true,
					displayMsg : '第 {0} - {1} 条  共 {2} 条',
					emptyMsg : "没有记录",
					listeners:{
						beforechange:function(pt,obj) {
							userLoginStore.recordStart = obj.start+1 ;
						    
						}
					}
				})]
			,columns:[selectMode,{
				        header:'用户名',
						sortable:true,
						dataIndex:"USER_NAME",
						width:50
					},{
				        header:'登录时间',
						sortable:true,
						dataIndex:"LOGIN_TIME",
						width:50
					},{
				        header:'登出时间',
						sortable:true,
						dataIndex:"LOGOUT_TIME",
						renderer:function(value, metaData, record, rowIndex, colIndex, store){
							if(record.data["LOGOUT_TIME"] == null){
								return "";
							}
						},
						width:50
					},{
				        header:'状态',
						sortable:true,
						dataIndex:"LOG_CONTENT",
						renderer:function(value, metaData, record, rowIndex, colIndex, store){
							if(record.data["LOG_CONTENT"] == null){
								return "";
							}
						},
						width:50
					},{
				        header:'登录IP',
						sortable:true,
						dataIndex:"IP",
						width:50
					}
			]
		}];
	}
	,doSearch: function(btn) {
		var startTime = btn.ownerCt.ownerCt.startTime.getValue();
		var endTime = btn.ownerCt.ownerCt.endTime.getValue();
   		var text = btn.ownerCt.ownerCt.searchText.getValue();
   		var store = btn.ownerCt.ownerCt.items.get(0).getStore();
   		if(startTime!=""){
   			startTime = startTime.format('Y-m-d');
   		}
   		if(endTime != ""){
   			endTime = endTime.format('Y-m-d');
   		}
   		
//   		store.proxy.setUrl('log_getSystemLogs.action',true);
   		store.baseParams = {'userName':text, 'startTime':startTime, 'endTime':endTime} ;
   		store.load({
			params:{
				start:0,
				limit:25
			}
		});
	}
	,deleteLog:function(btn) {
   		var gp = btn.ownerCt.ownerCt.items.get(0) ;
   		var rows= gp.getSelectionModel().getSelections(); 
   		if(rows.length==0) {
   			 Ext.Msg.alert("提示", "请选择要操作的日志!");
   			 return ;
   		}
   		var ids = "";
   		for(var i=0 ;i<rows.length;i++) {
   			if(i==0) {
   				ids = rows[i].data['ID'] ;
   			}else{
   				ids = ids +","+rows[i].data['ID'] ;
   			}
   		}
   		Ext.MessageBox.confirm("确认", "确认要删除这些日志吗?", function(btn) {
					if (btn == "yes") {
						Ext.Ajax.request( {
							waitMsg : '正在执行  ',
							method : 'POST',
							params : {
								ids:ids 
							},
							url : 'log_delSystemLogs.action',
							scope : this,
							success : function(request) {
								var Result=Ext.decode(request.responseText);
								if(Result.success) {
									gp.getStore().reload();
								} else {
									 Ext.Msg.alert("错误",Result.info );
								}
							},
							failure : function() {
								Ext.Msg.alert("错误", "与后台联系的时候出现了问题");
							}
						});
						
					}
				});
   	}
});
/**
 * 自定义 textField  添加双击事件
 */
LOG.userLogin.textField = Ext.extend(Ext.form.TextField,{
		initComponent: function(){
			Ext.form.TextField.superclass.initComponent.call(this);
			this.addEvents('dblclick');
		}
		,initEvents : function() {
			Ext.form.TextField.superclass.initEvents.call(this);
			this.mon(this.el, 'dblclick', this.onDblClick, this);
		}
	});
Ext.reg('logTextField',LOG.userLogin.textField);

/**
 * 有效员工查询
 */
LOG.userLogin.validStaff = Ext.extend(Ext.Window,{
	width:650
	,height:400
	,layout:'border'
	,title:'员工列表'
	,plain:true
	,modal:true
	,closeAction:'close'
	,limit:25
	,resizable:true
	,initComponent:function(){
		this.tbar = this.buildTbar(this);
		this.items = this.buildGrid();
		this.buttons = this.buildButton();
		LOG.userLogin.validStaff.superclass.initComponent.call(this);
	}
	,buildTbar: function(mp){
		return [{xtype:'spacer',width:10}
			,{xtype:'label',text:'员工姓名：'}
			,{
				xtype:'textfield'
				,width:200
				,name:'staffName'
				,emptyText:'请输入员工姓名'
				,ref: '../searchStaffText'
				,listeners:{
                 	 	'specialkey':function(f,e) {
                 	 		if(e.getKey() == e.ENTER){
								mp.doSearch(mp.searchStaffText);
                 	 		}
                 	 	}
                 	 }
			},'-'
			,{xtype:'spacer',width:10}
			,{
   				text : "查询",
   				ref:"../search",
   				handler :this.doSearch,
   				iconCls: 'ico-dot-search'
   			 }];
	}
	,buildGrid: function(){
   	 	var selectMode=new Ext.grid.CheckboxSelectionModel({
			dataIndex: "ID",
            sortable: false,
            singleSelect:true,
            width:20
		});
   	    var staffStore =new Ext.data.JsonStore({
	         totalProperty:'totalProperty'
        	 ,url:'log_getValidStaff.action'
        	 ,fields:['STAFF_ID','STAFF_NAME']
        	 ,root:'root'
        });
		return[{
			xtype:'grid'
			,region:'center'
			,viewConfig:{
				forceFit:true
			}
			,store:staffStore
			,sm:selectMode
			,bbar:[new Ext.PagingToolbar({
					pageSize : 25,
					store : staffStore,
					displayInfo : true,
					displayMsg : '第 {0} - {1} 条  共 {2} 条',
					emptyMsg : "没有记录",
					listeners:{
						beforechange:function(pt,obj) {
							staffStore.recordStart = obj.start+1 ;
						    
						}
					}
				})]
			,columns:[selectMode
				    ,{
				        header:'用户id',
						sortable:true,
						dataIndex:"STAFF_ID",
					}
					,{
				        header:'用户名',
						sortable:true,
						dataIndex:"STAFF_NAME",
					}
			]
		}];
	}
	,buildButton: function(){
		return [{
            text:'确认',
            listeners:{
         		click:function(btn) {
						var gp = btn.ownerCt.ownerCt.items.get(0) ;
   						var rows= gp.getSelectionModel().getSelections();
   						
   						if(rows.length==0) {
				   			 Ext.Msg.alert("提示", "请选择查询的员工id!");
				   			 return ;
				   		}else{
				   			gp.ownerCt.textField.setValue(rows[0].data['STAFF_ID']);
				   		}
   						
         				this.ownerCt.ownerCt.close();
         			}
         		}
            }
            ,{
             text:'关闭',
             listeners:{
             	click:function(n) {
             		this.ownerCt.ownerCt.close();
             	}
             }
            }
		];
	}
	,doSearch:function(btn){
//		alert(arguments[0]);
   		var text = btn.ownerCt.ownerCt.searchStaffText.getValue();
   		var store = btn.ownerCt.ownerCt.items.get(0).getStore();
   		
   		store.baseParams = {'userName':text} ;
   		store.load({
			params:{
				start:0,
				limit:25
			}
		});
	}
});